package com.founder.barcode.bussiness.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;

import com.founder.barcode.bussiness.advanced.mms.NMMSBodyEntity;
import com.founder.barcode.bussiness.util.BaseRequestService;
import com.founder.barcode.common.util.Base64Util;

public class Mt4Business extends BaseRequestService {
    
    @Override
    protected void init() {
        this.uri = BUSS_URL +  "/mt4business";
    }
    
    public String generateXml(String phoneNo,String businessId,String ruleId,
            String sendType,String type,String sText,String mText) throws Exception {
        String xml = this.getXml("/template/mt4business.xml");
        String transactionId = genTransactionId(businessId,null);
        String submitTime = DateFormatUtils.format(new Date(), "yyyyMMddHHmmss");
        Map<String, String> map = new HashMap<String, String>();
        map.put("TransactionID", transactionId);
        map.put("SubmitTime", submitTime);
        map.put("BusinessID", businessId);
        map.put("RuleID", ruleId);
        map.put("Msisdn", phoneNo);
        map.put("SendType", sendType);
        map.put("Type", type);
        map.put("SText", Base64Util.getBase64EncodeStr(sText));
        if(StringUtils.equals(type, "2")) {
            map.put("MText", this.generateMText());
        } else {
            map.put("MText", Base64Util.getBase64EncodeStr(mText));
        }
        xml = this.replaceXml(xml, map);
        return xml;
    }
    
    private List<NMMSBodyEntity> list = new ArrayList<NMMSBodyEntity>();
    
    public void addContent(String txtName,String txtContent,String imgName,String imgContent) throws Exception {
        Random random = new Random();
        boolean position = random.nextInt(1) == 0 ? false : true;
        NMMSBodyEntity entity = new NMMSBodyEntity();
        entity.setTxt_name(txtName);
        entity.setTxt_content(Base64Util.getBase64EncodeStr(txtContent));
        entity.setImg_name(imgName);
        entity.setImg_content(Base64Util.getBase64EncodeStr(imgContent));
        entity.setTxt_position(position);
        entity.setFrames(String.format("%d", list.size() + 1));
        list.add(entity);
    }
    
    public String generateMText() {
        NMMSBodyEntity entity = null;
        StringBuffer sb = new StringBuffer();
        for(int i = 0 ; i < list.size() ; i++) {
            entity = list.get(i);
            sb.append(entity.getFrames()).append("_").append(entity.getTxt_name())
                .append(",").append(entity.getTxt_content())
                .append(";")
                .append(entity.getImg_name()).append(",").append(entity.getImg_content())
                .append("#");
        }
        if(sb.length() > 0) {
            sb.deleteCharAt(sb.length()-1);
        }
        return sb.toString();
    }
    
    
    public String send(String phoneNo,String businessId,String ruleId,
            String sendType,String type,String sText,String mText) throws Exception {
        String xmlStr = this.generateXml(phoneNo,businessId,ruleId,sendType,type,sText,mText);
        String result = this.sendRequest(xmlStr);
        return result;
    }
}
